package cn.android.weblog;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import android.os.Message;
import cn.android.weblog.api.PassageAPI;
import cn.android.weblog.domain.Passage;
import cn.android.weblog.domain.User;
import cn.android.weblog.redisdata.CURRENT;

public class PassageDetailActivity extends AppCompatActivity {

    private Passage passage;
    private PassageAPI api;
    private TextView title,describe,author,time,context;
    private Button back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_passage_detail);

        api=new PassageAPI();
        title=(TextView)findViewById(R.id.detail_title);
        describe=(TextView)findViewById(R.id.detail_describe);
        author=(TextView)findViewById(R.id.detail_author);
        time=(TextView)findViewById(R.id.detail_time);
        context=(TextView)findViewById(R.id.detail_context);
        back=(Button)findViewById(R.id.detail_return);

        // 初始化页面
        initPage();

        // 点击作者进入作者详情页
        author.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                startActivity(new Intent(PassageDetailActivity.this,AuthorActivity.class));
            }
        });

        // 返回上一页
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }


    /**
     * 查询文章详情初始化页面
     */
    private void initPage(){
        // 当前选中的文章id：current.getPassageId()
        Map<String, Object> map = api.findPassageById(CURRENT.PASSAGE.getPassageId());
        // 获取文章详情
        passage= JSON.parseObject(map.get("passage").toString(),Passage.class);
        // 获取作者信息
        CURRENT.AUTHOR=JSON.parseObject(map.get("currentUser").toString(), User.class);

        // 给控件赋值
        title.setText(passage.getTitle());
        describe.setText(passage.getDescribed());
        author.setText(CURRENT.AUTHOR.getUsername());
        time.setText(passage.getCreateDate().toString());
        Spanned sp = Html.fromHtml(passage.getContent());
        context.setText(sp);

    }


}
